package com.student.liu.controller;

import com.student.liu.entity.Asset;
import com.student.liu.service.IAssetService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.Max;
import javax.validation.constraints.NotNull;
import java.util.Date;

@Api(tags = "资产相关接口")
@RestController
@RequestMapping("admin/asset")
public class AssetController extends BaseController{

    @Resource
    IAssetService assetService;

    @Data
    @ApiModel("资产列表参数实体")
    static class ListParamBody{

        @NotNull(message = "必须传递pageIndex")
        private Integer pageIndex;

        @ApiModelProperty("页大小，一页显示多少条数据")
        @Max(value = 20, message = "页大小不能超过20条")
        @NotNull(message = "必须传递pageSize")
        private Integer pageSize;

        @ApiModelProperty("部门ID")
        private Integer departmentId;

        @ApiModelProperty("资产编号")
        private String assetNo;

        @ApiModelProperty("资产名")
        private String asset;
        @ApiModelProperty("资产价值")
        private Integer nav;
        @ApiModelProperty("数量")
        private Integer number;
        @ApiModelProperty("登记时间")
        private Date registertime;
    }

    @PostMapping
    public Object list(){
        return success(null, assetService.listAsset());
    }

    @PostMapping("list")
    @ApiOperation("资产列表")
//    @PreAuthorize("hasAnyRole('ROLE_admin','ROLE_boss') OR hasAnyAuthority('ass_show')")              //当前请求者要么拥有user_show权限，要么拥有admin、boss这两个角色
    public Object index(@Valid @RequestBody AssetController.ListParamBody paramBody, BindingResult result) {
        return success(null,assetService.pageUserInfo(paramBody.getPageIndex(),
                paramBody.getPageSize(),paramBody.getDepartmentId(),paramBody.getAssetNo(),paramBody.getAsset(),paramBody.getNav(),paramBody.getNumber(),paramBody.getRegistertime()));
    }


    @PostMapping("save")
    @ApiOperation("保存资产")
    @PreAuthorize("hasAnyRole('ROLE_admin','ROLE_boss') OR hasAuthority('ass_save')")
    public Object save(@Valid @RequestBody Asset asset, BindingResult result) {
        return success(assetService.saveAsset(asset), null);
    }

    @PostMapping("remove")
    @ApiOperation("移除资产")
    @PreAuthorize("hasAnyRole('ROLE_admin','ROLE_boss') OR hasAuthority('ass_remove')")
    public Object remove(@RequestParam Integer id) {
        assetService.removeAsset(id);
        return success("移除成功", null);
    }

    @GetMapping("get/{id}")
    @ApiOperation("根据ID获取资产")
    @PreAuthorize("hasAnyRole('ROLE_admin','ROLE_boss')")
    public Object get(@PathVariable Integer id) {
        return success(null ,assetService.getById(id));
    }

}
